{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Check if Number Has Equal Digit Count and Digit Value"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #string #counting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #字符串 #计数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: digitCount"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #判断一个数的数字计数是否等于数位的值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始长度为 <code>n</code>&nbsp;的字符串&nbsp;<code>num</code>&nbsp;，它只包含数字。</p>\n",
    "\n",
    "<p>如果对于 <strong>每个</strong><em>&nbsp;</em><code>0 &lt;= i &lt; n</code>&nbsp;的下标&nbsp;<code>i</code>&nbsp;，都满足数位<em>&nbsp;</em><code>i</code>&nbsp;在 <code>num</code>&nbsp;中出现了&nbsp;<code>num[i]</code>次，那么请你返回&nbsp;<code>true</code>&nbsp;，否则返回&nbsp;<code>false</code>&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>num = \"1210\"\n",
    "<b>输出：</b>true\n",
    "<strong>解释：</strong>\n",
    "num[0] = '1' 。数字 0 在 num 中出现了一次。\n",
    "num[1] = '2' 。数字 1 在 num 中出现了两次。\n",
    "num[2] = '1' 。数字 2 在 num 中出现了一次。\n",
    "num[3] = '0' 。数字 3 在 num 中出现了零次。\n",
    "\"1210\" 满足题目要求条件，所以返回 true 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>num = \"030\"\n",
    "<b>输出：</b>false\n",
    "<strong>解释：</strong>\n",
    "num[0] = '0' 。数字 0 应该出现 0 次，但是在 num 中出现了两次。\n",
    "num[1] = '3' 。数字 1 应该出现 3 次，但是在 num 中出现了零次。\n",
    "num[2] = '0' 。数字 2 在 num 中出现了 0 次。\n",
    "下标 0 和 1 都违反了题目要求，所以返回 false 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == num.length</code></li>\n",
    "\t<li><code>1 &lt;= n &lt;= 10</code></li>\n",
    "\t<li><code>num</code>&nbsp;只包含数字。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [check-if-number-has-equal-digit-count-and-digit-value](https://leetcode.cn/problems/check-if-number-has-equal-digit-count-and-digit-value/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [check-if-number-has-equal-digit-count-and-digit-value](https://leetcode.cn/problems/check-if-number-has-equal-digit-count-and-digit-value/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"1210\"', '\"030\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        rec = {i: 0 for i in range(10)}\n",
    "        for n in num:\n",
    "            rec[int(n)] += 1\n",
    "        for i, n in enumerate(num):\n",
    "            if rec[i] != int(n):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        counter = Counter(num)\n",
    "        for i in range(len(num)):\n",
    "            if num[i] != str(counter[str(i)]):\n",
    "                return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        for i in range(len(num)):\n",
    "            n=num.count(str(i))\n",
    "            if int(num[i])!=n:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        h = Counter(num)\n",
    "        for idx, v in enumerate(num):\n",
    "            if h[str(idx)] != int(v):\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        for i,c in enumerate(num):\n",
    "            if int(c)!=num.count(str(i)):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        ct = defaultdict(int)\n",
    "        for i in range(len(num)):\n",
    "            if num[i] not in ct:\n",
    "                ct[num[i]] = 1\n",
    "            else:\n",
    "                ct[num[i]] += 1\n",
    "        for i in range(len(num)):\n",
    "\n",
    "            if int(num[i]) != ct[str(i)]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        counter = Counter(num)\n",
    "        for i in range(len(num)):\n",
    "            if num[i] != str(counter[str(i)]):\n",
    "                return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        c = Counter(num)\n",
    "        for i,v in c.items():\n",
    "            try:\n",
    "                if int(num[int(i)]) != v:\n",
    "                    return False\n",
    "            except:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        for i in range(len(num)):\n",
    "            if num.count(str(i))!=int(num[i]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        h = Counter(num)\n",
    "        for idx, v in enumerate(num):\n",
    "            if h[str(idx)] != int(v):\n",
    "                return False\n",
    "        return True\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        lst = [0] * 11\n",
    "        for n in num:\n",
    "            lst[int(n)] += 1\n",
    "        s = \"\".join([str(i) for i in lst])\n",
    "        return s.startswith(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        return all([num.count(str(i)) == int(c) for i, c in enumerate(num)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        n = len(num)\n",
    "        ans = [0] * 10\n",
    "        for i in range(n):\n",
    "            ans[int(num[i])] += 1\n",
    "        # print(ans)\n",
    "        return \"\".join([str(x) for x in ans[:n]]) == num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        num_count = Counter(num)\n",
    "        for i in range(len(num)):\n",
    "            if int(num[i]) != num_count[str(i)]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        for i in range(len(num)):\n",
    "            if num.count(str(i)) != int(num[i]):\n",
    "                return False\n",
    "        return True\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "\n",
    "        for i in range(len(num)):\n",
    "\n",
    "            if int(num[i]) != num.count(str(i)):\n",
    "                return False\n",
    "        \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        for i in range(len(num)):\n",
    "            if num.count(str(i))==int(num[i]):continue\n",
    "            else:return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        flag=True\n",
    "        for i in range(len(num)):\n",
    "            if str(num.count(str(i)))!=num[i]:\n",
    "                flag=False\n",
    "                break\n",
    "        return flag\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        dic = Counter(num)\n",
    "        n = len(num)\n",
    "        return all(dic[str(i)] == int(num[i]) for i in range(n))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        cnt = Counter(num)\n",
    "        return all(cnt[str(i)] == int(v) for i, v in enumerate(num))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        dic = Counter(num)\n",
    "        for i in range(len(num)):\n",
    "            if dic[str(i)] != int(num[i]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        c=Counter(num)\n",
    "        for i in range(len(num)):\n",
    "            if c[str(i)]!=int(num[i]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        \n",
    "        for m in range(len(num)):\n",
    "            n=0\n",
    "            for i,c in enumerate(num):\n",
    "                if int(c)==m:\n",
    "                    n=n+1\n",
    "            if int(num[m])!=n:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        h=Counter(num)\n",
    "        for idx,v in enumerate(num):\n",
    "            if h[str(idx)]!=int(v):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        from collections import Counter\n",
    "\n",
    "        d = Counter(num)\n",
    "        n = len(num)\n",
    "        # print(d)\n",
    "        # print(num)\n",
    "        for i in range(n):\n",
    "            if d[str(i)]!=int(num[i]):\n",
    "                # print(d[i])\n",
    "                # print(num[i])\n",
    "                # print(i)\n",
    "                return False\n",
    "        \n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "\n",
    "        for i in range(len(num)):\n",
    "            count = 0\n",
    "            for j in range(0, len(num)):\n",
    "                cur=num[j]\n",
    "                if i ==int(num[j]):\n",
    "                    count += 1\n",
    "            if count == int(num[i]):\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True#''.join('true')\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        _dict = {}\n",
    "        for n in num:\n",
    "            if int(n) in _dict:\n",
    "                _dict[int(n)] += 1\n",
    "            else:\n",
    "                _dict[int(n)] = 1\n",
    "        for i in range(len(num)):\n",
    "            if _dict.get(i,0) != int(num[i]):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import defaultdict\r\n",
    "\r\n",
    "class Solution:\r\n",
    "    def digitCount(self, num: str) -> bool:\r\n",
    "        num = [int(x) for x in num]\r\n",
    "\r\n",
    "        d = defaultdict(int)\r\n",
    "\r\n",
    "        for x in num:\r\n",
    "            d[x] += 1\r\n",
    "\r\n",
    "        for i, x in enumerate(num):\r\n",
    "            if x != d[i]:\r\n",
    "                return False\r\n",
    "        return True\r\n",
    "\r\n",
    "        \r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        flag = 0\n",
    "        for i in range(len(num)):\n",
    "            if num.count(str(i)) != int(num[i]):\n",
    "                flag = 1\n",
    "        if flag == 1:\n",
    "            return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        lst = [0] * 11\n",
    "        n = len(num)\n",
    "        for i in num:\n",
    "            lst[int(i)] += 1\n",
    "        return num == \"\".join([str(k) for k in lst[:n]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        h = Counter(num)\n",
    "        for idx, v in enumerate(num):\n",
    "            if h[str(idx)] != int(v):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        n = len(num)\n",
    "        c = collections.Counter(num)\n",
    "        for i in range(n):\n",
    "            if c.get(str(i), 0) != int(num[i]):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        zd = dict()\n",
    "        for i in num:\n",
    "            zd[i] = zd.get(i, 0) + 1\n",
    "        for i in range(len(num)):\n",
    "            if num[i] == \"0\":\n",
    "                if str(i) in zd:\n",
    "                    return False\n",
    "            elif str(i) not in zd:\n",
    "                return False\n",
    "            elif zd[str(i)] != int(num[i]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        obj = {\n",
    "            \"0\":0,\n",
    "            \"1\":0,\n",
    "            \"2\":0,\n",
    "            \"3\":0,\n",
    "            \"4\":0,\n",
    "            \"5\":0,\n",
    "            \"6\":0,\n",
    "            \"7\":0,\n",
    "            \"8\":0,\n",
    "            \"9\":0\n",
    "        }\n",
    "        for s in num:\n",
    "            obj[s] += 1\n",
    "\n",
    "        for i in range(len(num)):\n",
    "            if obj[str(i)] != int(num[i]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        for i,v in enumerate(num):\n",
    "            if num.count(str(i)) != int(v):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        _dict = {}\n",
    "        for n in num:\n",
    "            if int(n) in _dict:\n",
    "                _dict[int(n)] += 1\n",
    "            else:\n",
    "                _dict[int(n)] = 1\n",
    "        for i in range(len(num)):\n",
    "            if i not in _dict:\n",
    "                if num[i] != \"0\":\n",
    "                    return False\n",
    "                continue\n",
    "            if _dict[i] != int(num[i]):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        num=str(num)\n",
    "        a={}\n",
    "        for i in num:\n",
    "            if i in a:a[i]+=1\n",
    "            else:a[i]=1\n",
    "        for i in range(len(num)):\n",
    "            if str(i) in a:\n",
    "                if int(num[i])!=a[str(i)]:return 1==2\n",
    "            elif int(num[i])!=0:return 1==2\n",
    "        return 1==1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "\n",
    "        cnt=Counter(num)\n",
    "        for i,v in enumerate(num):\n",
    "            if cnt[str(i)]!=int(v):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        c = Counter(num)\n",
    "        for i in range(len(num)):\n",
    "            if c[str(i)] != int(num[i]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        flag = 0\n",
    "        i = 0\n",
    "        while flag == 0 and i < len(num):\n",
    "            if int(num[i]) != num.count(str(i)):\n",
    "                flag = 1\n",
    "                break\n",
    "            else:\n",
    "                i += 1\n",
    "        if flag == 0:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        cnt=Counter(num)\n",
    "        for i in range(len(num)):\n",
    "            if int(num[i])!=cnt[str(i)]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        \n",
    "        for i in range(len(num)):\n",
    "            if int(num[i]) != num.count(str(i)):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        dic = Counter(num)\n",
    "        return all(dic[str(i)] == int(num[i]) for i in range(len(num)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        cnt = [0] * 10\n",
    "        for i in range(len(num)):\n",
    "            cnt[int(num[i])] += 1\n",
    "        return [ int(x) for x in num.ljust(10, '0') ] == cnt\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        for i in range(len(num)):\n",
    "            if int(num[i])!=num.count(str(i)):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "\n",
    "        num_len = len(num)\n",
    "        for i in range(0, num_len):\n",
    "            if int(num[i]) != num.count(str(i)):\n",
    "                return False\n",
    "        return True "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        flag = 1\n",
    "        j = 0\n",
    "        for i in num:\n",
    "            if int(i) != num.count(str(j)):\n",
    "                flag = 0\n",
    "            j += 1\n",
    "        if flag == 1:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        x = len(num)\n",
    "        ans = 0\n",
    "        for i in range(x):\n",
    "            keys = int(num[i])\n",
    "            if num.count(str(i)) == keys:\n",
    "                ans += 1\n",
    "        if ans == x:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "\n",
    "        for i in range(len(num)):\n",
    "\n",
    "            if int(num[i]) != num.count(str(i)):\n",
    "                return False\n",
    "        \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        counter = Counter(num)\n",
    "        return all(counter[str(i)] == int(num[i]) for i in range(len(num)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        tmp=dict(Counter(num))\n",
    "        for i in range(len(num)):\n",
    "            if str(i) not in tmp:\n",
    "                if num[i]!=\"0\":\n",
    "                    return False\n",
    "                else:\n",
    "                    continue\n",
    "            if tmp[str(i)]!=int(num[i]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        for i in range(len(num)):\n",
    "            if num.count(str(i))!=int(num[i]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        for i in range(len(num)):\n",
    "            if int(num[i]) == num.count(str(i)):\n",
    "                continue\n",
    "            else:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        counter = Counter(num)\n",
    "        for i in range(len(num)):\n",
    "            if num[i] != str(counter[str(i)]):\n",
    "                return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        for i in range(len(num)):\n",
    "            if int(num[i])!=num.count(str(i)):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        # 使用Counter统计num中每个数字的出现次数\n",
    "        num_count = Counter(num)\n",
    "        \n",
    "        # 遍历num，检查每个数字是否出现了与其值相应的次数\n",
    "        for i in range(len(num)):\n",
    "            if int(num[i]) != num_count.get(str(i), 0):\n",
    "                return False\n",
    "                \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        # Counter\n",
    "        c = dict()\n",
    "        for ch in num:\n",
    "            if ch in c:\n",
    "                c[ch] += 1\n",
    "            else:\n",
    "                c[ch] = 1\n",
    "        n = len(num)\n",
    "        for i in range(n):\n",
    "            if str(i) in c and c[str(i)] != int(num[i]):\n",
    "                return False\n",
    "            elif str(i) not in c and int(num[i]) != 0:\n",
    "                return  False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        counter = Counter(num)\n",
    "        for i in range(len(num)):\n",
    "            if num[i] != str(counter[str(i)]):\n",
    "                return False\n",
    "        else:\n",
    "            return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        return all(int(num[i]) == num.count(str(i)) for i in range(len(num)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        tuple_num = tuple(num)\n",
    "        print(tuple_num)\n",
    "        for i in range(len(num)):\n",
    "            if int(num[i]) != tuple_num.count(str(i)):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        zd = dict()\n",
    "        for i in num:\n",
    "            zd[i] = zd.get(i, 0) + 1\n",
    "        for i in range(len(num)):\n",
    "            if num[i] == \"0\":\n",
    "                if str(i) in zd:\n",
    "                    return False\n",
    "            elif str(i) not in zd:\n",
    "                return False\n",
    "            elif zd[str(i)] != int(num[i]):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        l = list(num)\n",
    "\n",
    "        for n in range(0,len(l)):\n",
    "           \n",
    "            if int(l[n]) != l.count(str(n)):\n",
    "                return False\n",
    "\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        counter = dict(Counter(num))\n",
    "        for index, char in enumerate(num):\n",
    "            count = counter.get(str(index), 0)\n",
    "            if count and count != int(char):\n",
    "                return False\n",
    "            elif count != int(char):\n",
    "                return False\n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        return all(int(num[i]) == Counter(num)[str(i)] for i in range(len(num)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "\n",
    "        for i in range(len(num)):\n",
    "\n",
    "            if int(num[i]) != num.count(str(i)):\n",
    "                return False\n",
    "        \n",
    "        return True\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    \n",
    "\n",
    "\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        for i in range(len(num)):\n",
    "            if num.count(str(i))!=int(num[i]):\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        c = Counter(num)\n",
    "        for i,n in enumerate(num):\n",
    "            if c[str(i)]!=int(n):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        for i in range(len(num)):\n",
    "            if int(num[i]) != list(num).count(str(i)):\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        record = collections.defaultdict(int)\n",
    "        for c in num:\n",
    "            record[c] += 1\n",
    "        # print(record)\n",
    "        n = len(num)\n",
    "        for i in range(n):\n",
    "            if int(num[i]) != record[str(i)]:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        num_count = Counter(num)\n",
    "        hax = {str(i): int(j) for i, j in enumerate(num)}\n",
    "        return num_count == Counter(hax)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def digitCount(self, num: str) -> bool:\n",
    "        h = Counter(num)\n",
    "        for idx , v in enumerate(num):\n",
    "            if h[str(idx)] != int(v):\n",
    "                return False\n",
    "        return True"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
